package com.atguigu.gulimall.auth.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberRespVo;
import com.atguigu.gulimall.auth.feign.MemberFeignService;
import com.atguigu.gulimall.auth.feign.ThirdPartyFeignService;
import com.atguigu.gulimall.auth.vo.UserLoginVo;
import com.atguigu.gulimall.auth.vo.UserRegistVo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Controller
public class LoginController {
    final ThirdPartyFeignService thirdPartyFeignService;
    final StringRedisTemplate redisTemplate;
    final MemberFeignService memberFeignService;

    public LoginController(ThirdPartyFeignService thirdPartyFeignService, StringRedisTemplate redisTemplate, MemberFeignService memberFeignService) {
        this.thirdPartyFeignService = thirdPartyFeignService;
        this.redisTemplate = redisTemplate;
        this.memberFeignService = memberFeignService;
    }

    /**
     * 发送短信验证
     *
     * @param phone
     * @return
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/sms/sendcode")
    public R sendCode(@RequestParam("phone") String phone) throws Exception {
        String redisCode = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone);
        if (StrUtil.isNotEmpty(redisCode)) {
            long l = Long.parseLong(redisCode.split("_")[1]);
            if (System.currentTimeMillis() - l < 60000) {
                return R.error(BizCodeEnume.SMS_CODE_EXCEPTION.getCode(), BizCodeEnume.SMS_CODE_EXCEPTION.getMsg());
            }
        }
        int code = RandomUtil.randomInt(100000, 1000000);
        String codeNum = code + "_" + System.currentTimeMillis();
        redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone, codeNum, 10, TimeUnit.MINUTES);
        thirdPartyFeignService.sendCode(phone, code);
        return R.ok();
    }

    /**
     * 注册的控制器
     * RedirectAttributes 模拟重定向携带数据
     * 其原理：
     * 利用session原理，将数据放在session中，然后重定向到页面之后，再从session中取出来
     * 跳到下一个页面，然后取出数据以后，session里面的数据就会删掉，
     */
    @PostMapping("/regist")
    public String regist(@Valid UserRegistVo vo, BindingResult result, RedirectAttributes redirectAttributes) {
        if (result.hasErrors()) { //如果校验出错
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            //存放错误消息，让前端感知并获取错误信息
            redirectAttributes.addFlashAttribute("errors", errors);
            return "redirect:http://auth.gulimall.com/reg.html";//注册页
        }
        //1、校验验证码，从页面提交过来的验证码
        String code = vo.getCode();
        //获取到redis存的验证码
        String s = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + vo.getPhone());
        if (!StrUtil.isEmpty(s)) { //redis里面存了这个手机号对应的验证码
            //,验证码正确,就进行截串，拿到第一个数据然后进行对比
            if (code.equals(s.split("_")[0])) {
                //验证码通过以后，还必须要删除验证码,因为这样下次若还带着之前的验证码过来，就会验证失败
                redisTemplate.delete(AuthServerConstant.SMS_CODE_CACHE_PREFIX + vo.getPhone());
                //验证码通过，  如果上面校验并无出错，就进行注册 ，调用远程服务gulimall-member进行注册
                R r = memberFeignService.regist(vo);
                if (r.getCode() == 0) { //成功就返回登陆页
                    return "redirect:http://auth.gulimall.com/login.html";
                } else { //失败就返回注册页,并且显示错误消息
                    Map<String, String> errors = new HashMap<>();
                    errors.put("msg", r.getData("msg ", new TypeReference<String>() {
                    }));
                    redirectAttributes.addFlashAttribute("errors", errors);
                    return "redirect:http://auth.gulimall.com/reg.html";//注册页
                }
            } else { //验证不通过
                Map<String, String> errors = new HashMap<>();
                errors.put("code", "验证码错误");
                //存放错误消息，让前端感知并获取错误信息
                redirectAttributes.addFlashAttribute("errors", errors);
                return "redirect:http://auth.gulimall.com/reg.html";//注册页
            }
        } else {  //redis存的验证码已过期
            Map<String, String> errors = new HashMap<>();
            errors.put("code", "验证码错误");
            //存放错误消息，让前端感知并获取错误信息
            redirectAttributes.addFlashAttribute("errors", errors);
            return "redirect:http://auth.gulimall.com/reg.html";//注册页
        }
    }

    /**
     * 账号密码登录请求
     */
    @PostMapping("/login")
    public String login(UserLoginVo vo, RedirectAttributes redirectAttributes, HttpSession session) {
        //调用远程gulimall-member进行登录
        R login = memberFeignService.login(vo);
        if (login.getCode() == 0) {
            //成功登录
            //提取出用户信息,
            MemberRespVo data = login.getData("data", new TypeReference<>() {
            });
            //然后将提取出来的用户信息放到session中
            session.setAttribute(AuthServerConstant.LOGIN_USER, data);
            return "redirect:http://gulimall.com";
        } else {
            Map<String, String> errors = new HashMap<>();
            errors.put("msg", login.getData("msg", new TypeReference<String>() {
            }));//把错误消息放到map中
            redirectAttributes.addFlashAttribute("errors", errors); //提取出错误消息
            //失败就返回登录页
            return "redirect:http://auth.gulimall.com/login.html";
        }
    }

    /**
     * 登录成功后，再点击登录页不用再登录直接跳转到首页
     */
    @GetMapping("/login.html")
    public String loginPage(HttpSession session) {
        Object attribute = session.getAttribute(AuthServerConstant.LOGIN_USER);//拿到已登录的用户信息
        if (attribute == null) { //没登录，返回登录页
            return "login";
        } else { //登录了就直接跳回首页就行了
            return "redirect:http://gulimall.com";
        }

    }
}
